import java.util.ArrayList;

public class Board3D {
	
	int size;
	int board[][][];
	final int BLANK = 0;
	int numPieces;
	int numBlack;
	int numWhite;
	
	
	Board3D(int aSize)
	{
		
			size = aSize;
			board = new int[size][size][size];
			board[size/2-1][size/2][size/2-1] = 1;
			board[size/2][size/2-1][size/2-1] = 1;
			board[size/2-1][size/2-1][size/2] = 1;
			board[size/2][size/2][size/2] = 1;
			board[size/2-1][size/2-1][size/2-1] = -1;
			board[size/2][size/2][size/2-1] = -1;
			board[size/2-1][size/2][size/2] = -1;
			board[size/2][size/2-1][size/2] = -1;
                        
			numPieces = 8;
			numBlack = 4;
			numWhite = 4;	
    }
	
	void copy(Board3D b)
	{
		for(int i = 0; i < size; i++)
			for(int j = 0; j < size; j++)
				for(int k = 0; k < size; k++)
				this.board[i][j][k] = b.board[i][j][k];
		this.numBlack = b.numBlack;
		this.numWhite = b.numWhite;
		this.numPieces = b.numPieces;
	}
	
	int someoneWon()
	{
		if(numBlack == 0)
			return -1;	//player two wins
		else if(numWhite == 0)
			return 1;	//player one wins
		
		if(noMoreValidMoves(-1) && noMoreValidMoves(1))
		{
			if(numBlack > numWhite)
				return 1;
			else if(numBlack < numWhite)
				return -1;
			else
				return 2;	// Draw
		}
		return 0;	// No winner yet
	}
	
	void flip(int x, int y, int z)
	{
		board[x][y][z] = -board[x][y][z];
	}
	
	void flipAll(ArrayList<Integer> locationList)
	{
		for(int i =0; i<locationList.size(); i+=3)
		{
			flip(locationList.get(i).intValue(), locationList.get(i+1).intValue(), locationList.get(i+2).intValue());
		}
	}
	
	
	boolean isOnBoard(int x, int y, int z)
	{
		if(x<size && x>=0)
		{
			if(y<size && y>= 0)
			{
				if(z<size && z>=0)
				{
					return true;
				}
			}
		}
		return false;
	}
	//insert a piece into a 3d board
	void insertPiece(int x, int y, int z, int player)
	{
		int otherPlayer = -player;
		int offSetX, offSetY, offSetZ;
		ArrayList<Integer> toFlip = new ArrayList<Integer>();
		ArrayList<Integer> maybeFlip = new ArrayList<Integer>();
		
		//south east of xy, z stays same
		if(isOnBoard(x+1, y+1, z))
		{
			offSetX = 1;
			offSetY = 1;
			if(board[x+offSetX][y+offSetY][z] == otherPlayer)
			{
				maybeFlip.add(new Integer(x+offSetX));
				maybeFlip.add(new Integer(y+offSetY));
				maybeFlip.add(new Integer(z));
				offSetX +=1;
				offSetY +=1;
				while(isOnBoard(x+offSetX, y+offSetY, z) && (board[x+offSetX][y+offSetY][z] == otherPlayer ||
						board[x+offSetX][y+offSetY][z] == player))
				{
					if(board[x+offSetX][y+offSetY][z] == player)
					{
						toFlip.addAll(maybeFlip);
						break;
					}
					else
					{
						maybeFlip.add(new Integer(x+offSetX));
						maybeFlip.add(new Integer(y+offSetY));
						maybeFlip.add(new Integer(z));
						offSetX +=1;
						offSetY +=1;
					}
				}
				
				
			}
			maybeFlip.clear();
		}
		
		//south east of xy, z +1
		if(isOnBoard(x+1, y+1, z+1))
		{
			offSetX = 1;
			offSetY = 1;
			offSetZ = 1;
			if(board[x+offSetX][y+offSetY][z+offSetZ] == otherPlayer)
			{
				maybeFlip.add(new Integer(x+offSetX));
				maybeFlip.add(new Integer(y+offSetY));
				maybeFlip.add(new Integer(z+offSetZ));
				offSetX +=1;
				offSetY +=1;
				offSetZ +=1;
				while(isOnBoard(x+offSetX, y+offSetY, z+offSetZ) && (board[x+offSetX][y+offSetY][z+offSetZ] == otherPlayer ||
						board[x+offSetX][y+offSetY][z+offSetZ] == player))
				{
					if(board[x+offSetX][y+offSetY][z+offSetZ] == player)
					{
						toFlip.addAll(maybeFlip);
						break;
					}
					else
					{
						maybeFlip.add(new Integer(x+offSetX));
						maybeFlip.add(new Integer(y+offSetY));
						maybeFlip.add(new Integer(z+offSetZ));
						offSetX +=1;
						offSetY +=1;
						offSetZ +=1;
					}
				}
				
				
			}
			maybeFlip.clear();
		}
		
		//south east of xy, z-1
		if(isOnBoard(x+1, y+1, z-1))
		{
			offSetX = 1;
			offSetY = 1;
			offSetZ = -1;
			if(board[x+offSetX][y+offSetY][z+offSetZ] == otherPlayer)
			{
				maybeFlip.add(new Integer(x+offSetX));
				maybeFlip.add(new Integer(y+offSetY));
				maybeFlip.add(new Integer(z+offSetZ));
				offSetX +=1;
				offSetY +=1;
				offSetZ -=1;
				while(isOnBoard(x+offSetX, y+offSetY, z+offSetZ) && (board[x+offSetX][y+offSetY][z+offSetZ] == otherPlayer ||
						board[x+offSetX][y+offSetY][z+offSetZ] == player))
				{
					if(board[x+offSetX][y+offSetY][z+offSetZ] == player)
					{
						toFlip.addAll(maybeFlip);
						break;
					}
					else
					{
						maybeFlip.add(new Integer(x+offSetX));
						maybeFlip.add(new Integer(y+offSetY));
						maybeFlip.add(new Integer(z+offSetZ));
						offSetX +=1;
						offSetY +=1;
						offSetZ -=1;
					}
				}
				
				
			}
			maybeFlip.clear();
		}
		
		
		
		
		
		//east of xy, z stays same
		if(isOnBoard(x+1,y, z))
		{
			
			offSetX = 1;
			
			if(board[x+offSetX][y][z] == otherPlayer)
			{
				maybeFlip.add(new Integer(x+offSetX));
				maybeFlip.add(new Integer(y));
				maybeFlip.add(new Integer(z));
				offSetX +=1;				
				while(isOnBoard(x+offSetX, y, z) && (board[x+offSetX][y][z] == otherPlayer ||
						board[x+offSetX][y][z] == player))
				{
					if(board[x+offSetX][y][z] == player )
					{
						toFlip.addAll(maybeFlip);
						break;
					}
					else
					{
						maybeFlip.add(new Integer(x+offSetX));
						maybeFlip.add(new Integer(y));
						maybeFlip.add(new Integer(z));
						offSetX +=1;
					}
				}
				
				
			}
			maybeFlip.clear();
		}
		
		//east of xy, z +1
		if(isOnBoard(x+1,y, z+1))
		{
			
			offSetX = 1;
			offSetZ = 1;
			if(board[x+offSetX][y][z+offSetZ] == otherPlayer)
			{
				maybeFlip.add(new Integer(x+offSetX));
				maybeFlip.add(new Integer(y));
				maybeFlip.add(new Integer(z+offSetZ));
				offSetX +=1;
				offSetZ +=1;
				while(isOnBoard(x+offSetX, y, z+offSetZ) && (board[x+offSetX][y][z+offSetZ] == otherPlayer ||
						board[x+offSetX][y][z+offSetZ] == player))
				{
					if(board[x+offSetX][y][z+offSetZ] == player )
					{
						toFlip.addAll(maybeFlip);
						break;
					}
					else
					{
						maybeFlip.add(new Integer(x+offSetX));
						maybeFlip.add(new Integer(y));
						maybeFlip.add(new Integer(z+offSetZ));
						offSetX +=1;
						offSetZ +=1;
					}
				}
				
				
			}
			maybeFlip.clear();
		}
		
		//east of xy, z -1
		if(isOnBoard(x+1,y, z-1))
		{
			
			offSetX = 1;
			offSetZ = -1;
                        System.out.println((x+offSetX) + " " + (y) + " " + (z-offSetZ));
			if(board[x+offSetX][y][z+offSetZ] == otherPlayer)
			{
                             
				maybeFlip.add(new Integer(x+offSetX));
				maybeFlip.add(new Integer(y));
				maybeFlip.add(new Integer(z+offSetZ));
				offSetX +=1;
				offSetZ -=1;
				while(isOnBoard(x+offSetX, y, z+offSetZ) && (board[x+offSetX][y][z+offSetZ] == otherPlayer ||
						board[x+offSetX][y][z+offSetZ] == player))
				{
					if(board[x+offSetX][y][z+offSetZ] == player )
					{
						toFlip.addAll(maybeFlip);
						break;
					}
					else
					{
						maybeFlip.add(new Integer(x+offSetX));
						maybeFlip.add(new Integer(y));
						maybeFlip.add(new Integer(z+offSetZ));
						offSetX +=1;
						offSetZ -=1;
					}
				}
				
				
			}
			maybeFlip.clear();
		}
		
		//north east of xy, z stays same
		if(isOnBoard(x+1,y-1,z))
		{
			offSetX = 1;
			offSetY = -1;
			if(board[x+offSetX][y+offSetY][z] == otherPlayer)
			{
				maybeFlip.add(new Integer(x+offSetX));
				maybeFlip.add(new Integer(y+offSetY));
				maybeFlip.add(new Integer(z));
				offSetX +=1;
				offSetY -=1;
				while(isOnBoard(x+offSetX, y+offSetY,z) && (board[x+offSetX][y+offSetY][z] == otherPlayer ||
						board[x+offSetX][y+offSetY][z] == player))
				{
					if(board[x+offSetX][y+offSetY][z] == player)
					{
						toFlip.addAll(maybeFlip);
						break;
					}
					else
					{
						maybeFlip.add(new Integer(x+offSetX));
						maybeFlip.add(new Integer(y+offSetY));
						maybeFlip.add(new Integer(z));
						offSetX +=1;
						offSetY -=1;
					}
				}
				
				
			}
			maybeFlip.clear();
		}
		
		//north east of xy, z + 1
		if(isOnBoard(x+1,y-1,z +1))
		{
			offSetX = 1;
			offSetY = -1;
			offSetZ = 1;
			if(board[x+offSetX][y+offSetY][z+offSetZ] == otherPlayer)
			{
				maybeFlip.add(new Integer(x+offSetX));
				maybeFlip.add(new Integer(y+offSetY));
				maybeFlip.add(new Integer(z+offSetZ));
				offSetX +=1;
				offSetY -=1;
				offSetZ +=1;
				while(isOnBoard(x+offSetX, y+offSetY,z+offSetZ) && (board[x+offSetX][y+offSetY][z+offSetZ] == otherPlayer ||
						board[x+offSetX][y+offSetY][z+offSetZ] == player))
				{
					if(board[x+offSetX][y+offSetY][z+offSetZ] == player)
					{
						toFlip.addAll(maybeFlip);
						break;
					}
					else
					{
						maybeFlip.add(new Integer(x+offSetX));
						maybeFlip.add(new Integer(y+offSetY));
						maybeFlip.add(new Integer(z+offSetZ));
						offSetX +=1;
						offSetY -=1;
						offSetZ +=1;
					}
				}
				
				
			}
			maybeFlip.clear();
		}
		
		//north east of xy, z - 1
		if(isOnBoard(x+1,y-1,z-1))
		{
			offSetX = 1;
			offSetY = -1;
			offSetZ = -1;
			if(board[x+offSetX][y+offSetY][z+offSetZ] == otherPlayer)
			{
				maybeFlip.add(new Integer(x+offSetX));
				maybeFlip.add(new Integer(y+offSetY));
				maybeFlip.add(new Integer(z+offSetZ));
				offSetX +=1;
				offSetY -=1;
				offSetZ -=1;
				while(isOnBoard(x+offSetX, y+offSetY,z+offSetZ) && (board[x+offSetX][y+offSetY][z+offSetZ] == otherPlayer ||
						board[x+offSetX][y+offSetY][z+offSetZ] == player))
				{
					if(board[x+offSetX][y+offSetY][z+offSetZ] == player)
					{
						toFlip.addAll(maybeFlip);
						break;
					}
					else
					{
						maybeFlip.add(new Integer(x+offSetX));
						maybeFlip.add(new Integer(y+offSetY));
						maybeFlip.add(new Integer(z+offSetZ));
						offSetX +=1;
						offSetY -=1;
						offSetZ -=1;
					}
				}
				
				
			}
			maybeFlip.clear();
		}
		
		
		//north of xy, z stays same
		if(isOnBoard(x,y-1,z))
		{
			offSetY = -1;
			if(board[x][y+offSetY][z] == otherPlayer)
			{
				maybeFlip.add(new Integer(x));
				maybeFlip.add(new Integer(y+offSetY));
				maybeFlip.add(new Integer(z));
				offSetY -=1;
				while(isOnBoard(x, y+offSetY,z) && (board[x][y+offSetY][z] == otherPlayer ||
						board[x][y+offSetY][z] == player))
				{
					if(board[x][y+offSetY][z] == player)
					{
						toFlip.addAll(maybeFlip);
						break;
					}
					else
					{
						maybeFlip.add(new Integer(x));
						maybeFlip.add(new Integer(y+offSetY));
						maybeFlip.add(new Integer(z));
						offSetY -=1;
					}
				}
				
				
			}
			maybeFlip.clear();
		}
		
		//north of xy, z+1
		if(isOnBoard(x,y-1,z+1))
		{
			offSetY = -1;
			offSetZ = 1;
			if(board[x][y+offSetY][z+offSetZ] == otherPlayer)
			{
				maybeFlip.add(new Integer(x));
				maybeFlip.add(new Integer(y+offSetY));
				maybeFlip.add(new Integer(z+offSetZ));
				offSetY -=1;
				offSetZ +=1;
				while(isOnBoard(x, y+offSetY,z+offSetZ) && (board[x][y+offSetY][z+offSetZ] == otherPlayer ||
						board[x][y+offSetY][z+offSetZ] == player))
				{
					if(board[x][y+offSetY][z+offSetZ] == player)
					{
						toFlip.addAll(maybeFlip);
						break;
					}
					else
					{
						maybeFlip.add(new Integer(x));
						maybeFlip.add(new Integer(y+offSetY));
						maybeFlip.add(new Integer(z+offSetZ));
						offSetY -=1;
						offSetZ +=1;
					}
				}
				
				
			}
			maybeFlip.clear();
		}
		
		//north of xy, z-1
		if(isOnBoard(x,y-1,z-1))
		{
			offSetY = -1;
			offSetZ = -1;
			if(board[x][y+offSetY][z+offSetZ] == otherPlayer)
			{
				maybeFlip.add(new Integer(x));
				maybeFlip.add(new Integer(y+offSetY));
				maybeFlip.add(new Integer(z+offSetZ));
				offSetY -=1;
				offSetZ -=1;
				while(isOnBoard(x, y+offSetY,z+offSetZ) && (board[x][y+offSetY][z+offSetZ] == otherPlayer ||
						board[x][y+offSetY][z+offSetZ] == player))
				{
					if(board[x][y+offSetY][z+offSetZ] == player)
					{
						toFlip.addAll(maybeFlip);
						break;
					}
					else
					{
						maybeFlip.add(new Integer(x));
						maybeFlip.add(new Integer(y+offSetY));
						maybeFlip.add(new Integer(z+offSetZ));
						offSetY -=1;
						offSetZ -=1;
					}
				}
				
				
			}
			maybeFlip.clear();
		}
		
		//north west of xy, z stays same
		if(isOnBoard(x-1,y-1,z))
		{
			offSetX = -1;
			offSetY = -1;
			if(board[x+offSetX][y+offSetY][z] == otherPlayer)
			{
				maybeFlip.add(new Integer(x+offSetX));
				maybeFlip.add(new Integer(y+offSetY));
				maybeFlip.add(new Integer(z));
				offSetX -=1;
				offSetY -=1;
				while(isOnBoard(x+offSetX, y+offSetY,z) && (board[x+offSetX][y+offSetY][z] == otherPlayer ||
						board[x+offSetX][y+offSetY][z] == player))
				{
					if(board[x+offSetX][y+offSetY][z] == player)
					{
						toFlip.addAll(maybeFlip);
						break;
					}
					else
					{
						maybeFlip.add(new Integer(x+offSetX));
						maybeFlip.add(new Integer(y+offSetY));
						maybeFlip.add(new Integer(z));
						offSetX -=1;
						offSetY -=1;
					}
				}
				
				
			}
			maybeFlip.clear();
		}
		
		//north west of xy, z+1
		if(isOnBoard(x-1,y-1,z+1))
		{
			offSetX = -1;
			offSetY = -1;
			offSetZ = 1;
			if(board[x+offSetX][y+offSetY][z+offSetZ] == otherPlayer)
			{
				maybeFlip.add(new Integer(x+offSetX));
				maybeFlip.add(new Integer(y+offSetY));
				maybeFlip.add(new Integer(z+offSetZ));
				offSetX -=1;
				offSetY -=1;
				offSetZ +=1;
				while(isOnBoard(x+offSetX, y+offSetY,z+offSetZ) && (board[x+offSetX][y+offSetY][z+offSetZ] == otherPlayer ||
						board[x+offSetX][y+offSetY][z+offSetZ] == player))
				{
					if(board[x+offSetX][y+offSetY][z+offSetZ] == player)
					{
						toFlip.addAll(maybeFlip);
						break;
					}
					else
					{
						maybeFlip.add(new Integer(x+offSetX));
						maybeFlip.add(new Integer(y+offSetY));
						maybeFlip.add(new Integer(z+offSetZ));
						offSetX -=1;
						offSetY -=1;
						offSetZ +=1;
					}
				}
				
				
			}
			maybeFlip.clear();
		}
		
		//north west of xy, z-1
		if(isOnBoard(x-1,y-1,z-1))
		{
			offSetX = -1;
			offSetY = -1;
			offSetZ = -1;
			if(board[x+offSetX][y+offSetY][z+offSetZ] == otherPlayer)
			{
				maybeFlip.add(new Integer(x+offSetX));
				maybeFlip.add(new Integer(y+offSetY));
				maybeFlip.add(new Integer(z+offSetZ));
				offSetX -=1;
				offSetY -=1;
				offSetZ -=1;
				while(isOnBoard(x+offSetX, y+offSetY,z+offSetZ) && (board[x+offSetX][y+offSetY][z+offSetZ] == otherPlayer ||
						board[x+offSetX][y+offSetY][z+offSetZ] == player))
				{
					if(board[x+offSetX][y+offSetY][z+offSetZ] == player)
					{
						toFlip.addAll(maybeFlip);
						break;
					}
					else
					{
						maybeFlip.add(new Integer(x+offSetX));
						maybeFlip.add(new Integer(y+offSetY));
						maybeFlip.add(new Integer(z+offSetZ));
						offSetX -=1;
						offSetY -=1;
						offSetZ -=1;
					}
				}
				
				
			}
			maybeFlip.clear();
		}
		
		//south west of xy, z stays same
		if(isOnBoard(x-1,y+1,z))
		{
			offSetX = -1;
			offSetY = 1;
			if(board[x+offSetX][y+offSetY][z] == otherPlayer)
			{
				maybeFlip.add(new Integer(x+offSetX));
				maybeFlip.add(new Integer(y+offSetY));
				maybeFlip.add(new Integer(z));
				offSetX -=1;
				offSetY +=1;
				while(isOnBoard(x+offSetX, y+offSetY,z) && (board[x+offSetX][y+offSetY][z] == otherPlayer ||
						board[x+offSetX][y+offSetY][z] == player))
				{
					if(board[x+offSetX][y+offSetY][z] == player)
					{
						toFlip.addAll(maybeFlip);
						break;
					}
					else
					{
						maybeFlip.add(new Integer(x+offSetX));
						maybeFlip.add(new Integer(y+offSetY));
						maybeFlip.add(new Integer(z));
						offSetX -=1;
						offSetY +=1;
					}
				}
				
				
			}
			maybeFlip.clear();
		}
		
		//south west of xy, z +1
		if(isOnBoard(x-1,y+1,z+1))
		{
			offSetX = -1;
			offSetY = 1;
			offSetZ = 1;
			if(board[x+offSetX][y+offSetY][z+offSetZ] == otherPlayer)
			{
				maybeFlip.add(new Integer(x+offSetX));
				maybeFlip.add(new Integer(y+offSetY));
				maybeFlip.add(new Integer(z+offSetZ));
				offSetX -=1;
				offSetY +=1;
				offSetZ +=1;
				while(isOnBoard(x+offSetX, y+offSetY,z+offSetZ) && (board[x+offSetX][y+offSetY][z+offSetZ] == otherPlayer ||
						board[x+offSetX][y+offSetY][z+offSetZ] == player))
				{
					if(board[x+offSetX][y+offSetY][z+offSetZ] == player)
					{
						toFlip.addAll(maybeFlip);
						break;
					}
					else
					{
						maybeFlip.add(new Integer(x+offSetX));
						maybeFlip.add(new Integer(y+offSetY));
						maybeFlip.add(new Integer(z));
						offSetX -=1;
						offSetY +=1;
						offSetZ +=1;
					}
				}
				
				
			}
			maybeFlip.clear();
		}
		
		//south west of xy, z -1
		if(isOnBoard(x-1,y+1,z-1))
		{
			offSetX = -1;
			offSetY = 1;
			offSetZ = -1;
			if(board[x+offSetX][y+offSetY][z+offSetZ] == otherPlayer)
			{
				maybeFlip.add(new Integer(x+offSetX));
				maybeFlip.add(new Integer(y+offSetY));
				maybeFlip.add(new Integer(z+offSetZ));
				offSetX -=1;
				offSetY +=1;
				offSetZ -=1;
				while(isOnBoard(x+offSetX, y+offSetY,z+offSetZ) && (board[x+offSetX][y+offSetY][z+offSetZ] == otherPlayer ||
						board[x+offSetX][y+offSetY][z+offSetZ] == player))
				{
					if(board[x+offSetX][y+offSetY][z+offSetZ] == player)
					{
						toFlip.addAll(maybeFlip);
						break;
					}
					else
					{
						maybeFlip.add(new Integer(x+offSetX));
						maybeFlip.add(new Integer(y+offSetY));
						maybeFlip.add(new Integer(z));
						offSetX -=1;
						offSetY +=1;
						offSetZ -=1;
					}
				}
				
				
			}
			maybeFlip.clear();
		}
		
		//south of xy z stays same
		if(isOnBoard(x,y+1,z))
		{

			offSetY = 1;
			if(board[x][y+offSetY][z] == otherPlayer)
			{
				maybeFlip.add(new Integer(x));
				maybeFlip.add(new Integer(y+offSetY));
				maybeFlip.add(new Integer(z));

				offSetY +=1;
				while(isOnBoard(x, y+offSetY,z) && (board[x][y+offSetY][z] == otherPlayer ||
						board[x][y+offSetY][z] == player))
				{
					if(board[x][y+offSetY][z] == player)
					{
						toFlip.addAll(maybeFlip);
						break;
					}
					else
					{
						maybeFlip.add(new Integer(x));
						maybeFlip.add(new Integer(y+offSetY));
						maybeFlip.add(new Integer(z));
						offSetY +=1;
					}
				}
				
				
			}
			maybeFlip.clear();
		}
		
		//south of xy z +1
		if(isOnBoard(x,y+1,z+1))
		{

			offSetY = 1;
			offSetZ = 1;
			if(board[x][y+offSetY][z+offSetZ] == otherPlayer)
			{
				maybeFlip.add(new Integer(x));
				maybeFlip.add(new Integer(y+offSetY));
				maybeFlip.add(new Integer(z+offSetZ));

				offSetY +=1;
				offSetZ +=1;
				while(isOnBoard(x, y+offSetY, z+offSetZ) && (board[x][y+offSetY][z] == otherPlayer ||
						board[x][y+offSetY][z] == player))
				{
					if(board[x][y+offSetY][z] == player)
					{
						toFlip.addAll(maybeFlip);
						break;
					}
					else
					{
						maybeFlip.add(new Integer(x));
						maybeFlip.add(new Integer(y+offSetY));
						maybeFlip.add(new Integer(z+offSetZ));
						offSetY +=1;
						offSetZ +=1;
					}
				}
				
				
			}
			maybeFlip.clear();
		}
		
		//south of xy z -1
		if(isOnBoard(x,y+1,z-1))
		{

			offSetY = 1;
			offSetZ = -1;
			if(board[x][y+offSetY][z+offSetZ] == otherPlayer)
			{
				maybeFlip.add(new Integer(x));
				maybeFlip.add(new Integer(y+offSetY));
				maybeFlip.add(new Integer(z+offSetZ));

				offSetY +=1;
				offSetZ -=1;
				while(isOnBoard(x, y+offSetY, z+offSetZ) && (board[x][y+offSetY][z] == otherPlayer ||
						board[x][y+offSetY][z] == player))
				{
					if(board[x][y+offSetY][z] == player)
					{
						toFlip.addAll(maybeFlip);
						break;
					}
					else
					{
						maybeFlip.add(new Integer(x));
						maybeFlip.add(new Integer(y+offSetY));
						maybeFlip.add(new Integer(z+offSetZ));
						offSetY +=1;
						offSetZ -=1;
					}
				}
				
				
			}
			maybeFlip.clear();
		}
		
		
		//west of xy, z stays same
		if(isOnBoard(x-1,y,z))
		{
			offSetX = -1;
			if(board[x+offSetX][y][z] == otherPlayer)
			{
				maybeFlip.add(new Integer(x+offSetX));
				maybeFlip.add(new Integer(y));
				maybeFlip.add(new Integer(z));
				offSetX -=1;
				while(isOnBoard(x+offSetX, y,z) && (board[x+offSetX][y][z] == otherPlayer ||
						board[x+offSetX][y][z] == player))
				{
					if(board[x+offSetX][y][z] == player)
					{
						toFlip.addAll(maybeFlip);
						break;
					}
					else
					{
						maybeFlip.add(new Integer(x+offSetX));
						maybeFlip.add(new Integer(y));
						maybeFlip.add(new Integer(z));
						offSetX -=1;
					}
				}
				
				
			}
			maybeFlip.clear();
		}
		
		//west of xy, z +1
		if(isOnBoard(x-1,y,z+1))
		{
			offSetX = -1;
			offSetZ = 1;
			if(board[x+offSetX][y][z+offSetZ] == otherPlayer)
			{
				maybeFlip.add(new Integer(x+offSetX));
				maybeFlip.add(new Integer(y));
				maybeFlip.add(new Integer(z+offSetZ));
				offSetX -=1;
				offSetZ +=1;
				while(isOnBoard(x+offSetX, y,z+offSetZ) && (board[x+offSetX][y][z+offSetZ] == otherPlayer ||
						board[x+offSetX][y][z+offSetZ] == player))
				{
					if(board[x+offSetX][y][z+offSetZ] == player)
					{
						toFlip.addAll(maybeFlip);
						break;
					}
					else
					{
						maybeFlip.add(new Integer(x+offSetX));
						maybeFlip.add(new Integer(y));
						maybeFlip.add(new Integer(z+offSetZ));
						offSetX -=1;
						offSetZ +=1;
					}
				}
				
				
			}
			maybeFlip.clear();
		}
		
		//west of xy, z -1
		if(isOnBoard(x-1,y,z-1))
		{
			offSetX = -1;
			offSetZ = -1;
			if(board[x+offSetX][y][z+offSetZ] == otherPlayer)
			{
				maybeFlip.add(new Integer(x+offSetX));
				maybeFlip.add(new Integer(y));
				maybeFlip.add(new Integer(z+offSetZ));
				offSetX -=1;
				offSetZ -=1;
				while(isOnBoard(x+offSetX, y,z+offSetZ) && (board[x+offSetX][y][z+offSetZ] == otherPlayer ||
						board[x+offSetX][y][z+offSetZ] == player))
				{
					if(board[x+offSetX][y][z+offSetZ] == player)
					{
						toFlip.addAll(maybeFlip);
						break;
					}
					else
					{
						maybeFlip.add(new Integer(x+offSetX));
						maybeFlip.add(new Integer(y));
						maybeFlip.add(new Integer(z+offSetZ));
						offSetX -=1;
						offSetZ -=1;
					}
				}
				
				
			}
			maybeFlip.clear();
		}
		
		//add the piece
		board[x][y][z] = player;
		if(player == 1)
		{
			numBlack += toFlip.size()+1;
			numWhite -= toFlip.size()-1;
		}
		if(player == -1)
		{
			numBlack -= toFlip.size()-1;
			numWhite += toFlip.size()+1;
		}
		//flip all valid pieces
		flipAll(toFlip);
		numPieces++;

	}
	
	boolean isValidMove(int x, int y, int z, int player)
	{
		if(board[x][y][z] != 0)
			return false;

    // 2D guys
    // ------------------------------------------------------------
		// check spot above
		if( y-1 >= 0 )
		{
			int tempy = y;
			if(board[x][--tempy][z] == -player)
			{
				if( tempy-1 >= 0 )
				{
          while (board[x][--tempy][z] != BLANK)
					{
            if (board[x][tempy][z] == player)
							return true;
						if( !(tempy-1 >= 0) )
							break;
					}
				}
			}
		}
		// check spot below
		if( y+1 < size )
		{
			int tempy = y;
      if (board[x][++tempy][z] == -player)
			{
				if( tempy+1 < size )
				{
          while (board[x][++tempy][z] != BLANK)
					{
            if (board[x][tempy][z] == player)
							return true;
						if( !(tempy+1 < size) )
							break;
					}
				}
			}
		}
		// check spot left
		if( x-1 >= 0 )
		{
			int tempx = x;
      if (board[--tempx][y][z] == -player)
			{
				if( tempx-1 >= 0 )
				{
          while (board[--tempx][y][z] != BLANK)
					{
            if (board[tempx][y][z] == player)
							return true;
						if( !(tempx-1 >= 0) )
							break;
					}
				}
			}
		}
		// check spot right
		if( x+1 < size )
		{
			int tempx = x;
      if (board[++tempx][y][z] == -player)
			{
				if( tempx+1 < size )
				{
          while (board[++tempx][y][z] != BLANK)
					{
            if (board[tempx][y][z] == player)
							return true;
						if( !(tempx+1 < size) )
							break;
					}
				}
			}
		}
		// check spot up-left
		if( y-1 >= 0 && x-1 >= 0)
		{
			int tempx = x;
			int tempy = y;
      if (board[--tempx][--tempy][z] == -player)
			{
				if( tempy-1 >= 0 && tempx-1 >= 0 )
				{
          while (board[--tempx][--tempy][z] != BLANK)
					{
            if (board[tempx][tempy][z] == player)
							return true;
						if( !(tempy-1 >= 0 && tempx-1 >= 0) )
							break;
					}
				}
			}
		}
		// check spot up-right
		if( y-1 >= 0 && x+1 < size)
		{
			int tempx = x;
			int tempy = y;
      if (board[++tempx][--tempy][z] == -player)
			{
				if( tempy-1 >= 0 && tempx+1 < size )
				{
          while (board[++tempx][--tempy][z] != BLANK)
					{
            if (board[tempx][tempy][z] == player)
							return true;
						if( !(tempy-1 >= 0 && tempx+1 < size) )
							break;
					}
				}
			}
		}
		// check spot bottom-left
		if( y+1 < size && x-1 >= 0 )
		{
			int tempx = x;
			int tempy = y;
      if (board[--tempx][++tempy][z] == -player)
			{
				if( tempy+1 < size && tempx-1 >= 0 )
				{
          while (board[--tempx][++tempy][z] != BLANK)
	 				{
            if (board[tempx][tempy][z] == player)
							return true;
						if( !(tempy+1 < size && tempx-1 >= 0) )
							break;
					}
				}
			}
		}
		// check spot bottom-right
		if( y+1 < size && x+1 < size )
		{
			int tempx = x;
			int tempy = y;
      if (board[++tempx][++tempy][z] == -player)
			{
				if( tempy+1 < size && tempx+1 < size )
				{
          while (board[++tempx][++tempy][z] != BLANK)
					{
            if (board[tempx][tempy][z] == player)
							return true;
						
						if( !(tempy+1 < size && tempx+1 < size) )
							break;
					}
				}
			}
		}

    // 3D guys
    // ------------------------------------------------------------
    // check spot above-forward
    if (y - 1 >= 0 && z - 1 >= 0)
    {
      int tempy = y;
      int tempz = z;
      if (board[x][--tempy][--tempz] == -player)
      {
        if (tempy - 1 >= 0 && tempz - 1 >= 0)
        {
          while (board[x][--tempy][--tempz] != BLANK)
          {
            if (board[x][tempy][tempz] == player)
              return true;
            if (!(tempy - 1 >= 0 && tempz-1 >= 0))
              break;
          }
        }
      }
    }
    // check spot below-forward
    if (y + 1 < size && z - 1 >= 0)
    {
      int tempy = y;
      int tempz = z;
      if (board[x][++tempy][--tempz] == -player)
      {
        if (tempy + 1 < size && tempz - 1 >= 0)
        {
          while (board[x][++tempy][--tempz] != BLANK)
          {
            if (board[x][tempy][tempz] == player)
              return true;
            if (!(tempy + 1 < size && tempz - 1 >= 0))
              break;
          }
        }
      }
    }
    // check spot left-forward
    if (x - 1 >= 0 && z - 1 >= 0)
    {
      int tempx = x;
      int tempz = z;
      if (board[--tempx][y][--tempz] == -player)
      {
        if (tempx - 1 >= 0 && tempz - 1 >= 0)
        {
          while (board[--tempx][y][--tempz] != BLANK)
          {
            if (board[tempx][y][tempz] == player)
              return true;
            if (!(tempx - 1 >= 0 && tempz - 1 >= 0))
              break;
          }
        }
      }
    }
    // check spot right-forward
    if (x + 1 < size && z - 1 >= 0)
    {
      int tempx = x;
      int tempz = z;
      if (board[++tempx][y][--tempz] == -player)
      {
        if (tempx + 1 < size && tempz - 1 >= 0)
        {
          while (board[++tempx][y][--tempz] != BLANK)
          {
            if (board[tempx][y][tempz] == player)
              return true;
            if (!(tempx + 1 < size && tempz - 1 >= 0))
              break;
          }
        }
      }
    }
    // check spot up-left-forward
    if (y - 1 >= 0 && x - 1 >= 0 && z - 1 >= 0)
    {
      int tempx = x;
      int tempy = y;
      int tempz = z;
      if (board[--tempx][--tempy][--tempz] == -player)
      {
        if (tempy - 1 >= 0 && tempx - 1 >= 0 && tempz - 1 >= 0)
        {
          while (board[--tempx][--tempy][--tempz] != BLANK)
          {
            if (board[tempx][tempy][tempz] == player)
              return true;
            if (!(tempy - 1 >= 0 && tempx - 1 >= 0 && tempz - 1 >= 0))
              break;
          }
        }
      }
    }
    // check spot up-right-forward
    if (y - 1 >= 0 && x + 1 < size && z - 1 >= 0)
    {
      int tempx = x;
      int tempy = y;
      int tempz = z;
      if (board[++tempx][--tempy][--tempz] == -player)
      {
        if (tempy - 1 >= 0 && tempx + 1 < size && tempz - 1 >= 0)
        {
          while (board[++tempx][--tempy][--tempz] != BLANK)
          {
            if (board[tempx][tempy][tempz] == player)
              return true;
            if (!(tempy - 1 >= 0 && tempx + 1 < size && tempz - 1 >= 0))
              break;
          }
        }
      }
    }
    // check spot bottom-left-forward
    if (y + 1 < size && x - 1 >= 0 && z - 1 >= 0)
    {
      int tempx = x;
      int tempy = y;
      int tempz = z;
      if (board[--tempx][++tempy][--tempz] == -player)
      {
        if (tempy + 1 < size && tempx - 1 >= 0 && tempz - 1 >= 0)
        {
          while (board[--tempx][++tempy][--tempz] != BLANK)
          {
            if (board[tempx][tempy][tempz] == player)
              return true;
            if (!(tempy + 1 < size && tempx - 1 >= 0 && tempz - 1 >= 0))
              break;
          }
        }
      }
    }
    // check spot bottom-right-forward
    if (y + 1 < size && x + 1 < size && z - 1 >= 0)
    {
      int tempx = x;
      int tempy = y;
      int tempz = z;
      if (board[++tempx][++tempy][--tempz] == -player)
      {
        if (tempy + 1 < size && tempx + 1 < size && tempz - 1 >= 0)
        {
          while (board[++tempx][++tempy][--tempz] != BLANK)
          {
            if (board[tempx][tempy][tempz] == player)
              return true;

            if (!(tempy + 1 < size && tempx + 1 < size && tempz - 1 >= 0))
              break;
          }
        }
      }
    }

    // check spot above-backward
    if (y - 1 >= 0 && z + 1 < size)
    {
      int tempy = y;
      int tempz = z;
      if (board[x][--tempy][++tempz] == -player)
      {
        if (tempy - 1 >= 0 && tempz + 1 < size)
        {
          while (board[x][--tempy][++tempz] != BLANK)
          {
            if (board[x][tempy][tempz] == player)
              return true;
            if (!(tempy - 1 >= 0 && tempz + 1 < size))
              break;
          }
        }
      }
    }
    // check spot below-backward
    if (y + 1 < size && z + 1 < size)
    {
      int tempy = y;
      int tempz = z;
      if (board[x][++tempy][++tempz] == -player)
      {
        if (tempy + 1 < size && tempz + 1 < size)
        {
          while (board[x][++tempy][++tempz] != BLANK)
          {
            if (board[x][tempy][tempz] == player)
              return true;
            if (!(tempy + 1 < size && tempz + 1 < size))
              break;
          }
        }
      }
    }
    // check spot left-backward
    if (x - 1 >= 0 && z + 1 < size)
    {
      int tempx = x;
      int tempz = z;
      if (board[--tempx][y][++tempz] == -player)
      {
        if (tempx - 1 >= 0 && tempz + 1 < size)
        {
          while (board[--tempx][y][++tempz] != BLANK)
          {
            if (board[tempx][y][tempz] == player)
              return true;
            if (!(tempx - 1 >= 0 && tempz + 1 < size))
              break;
          }
        }
      }
    }
    // check spot right-backward
    if (x + 1 < size && z + 1 < size)
    {
      int tempx = x;
      int tempz = z;
      if (board[++tempx][y][++tempz] == -player)
      {
        if (tempx + 1 < size && tempz + 1 < size)
        {
          while (board[++tempx][y][++tempz] != BLANK)
          {
            if (board[tempx][y][tempz] == player)
              return true;
            if (!(tempx + 1 < size && tempz + 1 < size))
              break;
          }
        }
      }
    }
    // check spot up-left-backward
    if (y - 1 >= 0 && x - 1 >= 0 && z + 1 < size)
    {
      int tempx = x;
      int tempy = y;
      int tempz = z;
      if (board[--tempx][--tempy][++tempz] == -player)
      {
        if (tempy - 1 >= 0 && tempx - 1 >= 0 && tempz + 1 < size)
        {
          while (board[--tempx][--tempy][++tempz] != BLANK)
          {
            if (board[tempx][tempy][tempz] == player)
              return true;
            if (!(tempy - 1 >= 0 && tempx - 1 >= 0 && tempz + 1 < size))
              break;
          }
        }
      }
    }
    // check spot up-right-backward
    if (y - 1 >= 0 && x + 1 < size && z + 1 < size)
    {
      int tempx = x;
      int tempy = y;
      int tempz = z;
      if (board[++tempx][--tempy][++tempz] == -player)
      {
        if (tempy - 1 >= 0 && tempx + 1 < size && tempz + 1 < size)
        {
          while (board[++tempx][--tempy][++tempz] != BLANK)
          {
            if (board[tempx][tempy][tempz] == player)
              return true;
            if (!(tempy - 1 >= 0 && tempx + 1 < size && tempz + 1 < size))
              break;
          }
        }
      }
    }
    // check spot bottom-left-backward
    if (y + 1 < size && x - 1 >= 0 && z + 1 < size)
    {
      int tempx = x;
      int tempy = y;
      int tempz = z;
      if (board[--tempx][++tempy][++tempz] == -player)
      {
        if (tempy + 1 < size && tempx - 1 >= 0 && tempz + 1 < size)
        {
          while (board[--tempx][++tempy][++tempz] != BLANK)
          {
            if (board[tempx][tempy][tempz] == player)
              return true;
            if (!(tempy + 1 < size && tempx - 1 >= 0 && tempz + 1 < size))
              break;
          }
        }
      }
    }
    // check spot bottom-right-backward
    if (y + 1 < size && x + 1 < size && z + 1 < size)
    {
      int tempx = x;
      int tempy = y;
      int tempz = z;
      if (board[++tempx][++tempy][++tempz] == -player)
      {
        if (tempy + 1 < size && tempx + 1 < size && tempz + 1 < size)
        {
          while (board[++tempx][++tempy][++tempz] != BLANK)
          {
            if (board[tempx][tempy][tempz] == player)
              return true;

            if (!(tempy + 1 < size && tempx + 1 < size && tempz + 1 < size))
              break;
          }
        }
      }
    }
		
		return false;
		
	}
	
	boolean noMoreValidMoves(int player)
	{
		for(int i = 0; i < size; i++)
			for(int j = 0; j < size; j++)
        for(int k = 0; k < size; k++)
				  if(isValidMove(i, j, k, player))
					  return false;
		return true;
	}
		
}
	
	